package com.soft.hwx.scdapp.msg;

import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Arrays;



/*
    工作指令 消息
    55 aa 01 02 03
 */

/*
            附表3 侦察主机工作状态和工作结果汇总表
***************************************************************************************************************
*   序号  *     工作指令          *     工作状态    *   结果1          *        结果2       *       结果3       *
*    1    *      0101(自检)       *      01        *    A1 正在自检    *     B1 自检正常    *     F1 自检故障   *
*    2    *      0202(参数装订)   *      02        *    A2  正在装订   *     B2  装订好     *     F2 装订故障   *
*    3    *      0303(搜索)       *      03        *    A3  正在搜索   *     B3  跟踪       *     F3  丢失     *
*    4    *      0404(放弃搜索)    *     04        *                   *     B4 放弃成功    *                  *
****************************************************************************************************************
 */

public class RawMsg  {
    protected   int      sn;      //序号
    protected   long      st;      //时间戳


    protected   byte[]  buf;

    //主机消息公共字段
    public  byte[] frameHead = {(byte)0x55,(byte)0xAA};
    public byte  length;
    public byte checkSum; //校验和（checksum）是数据长度与数据包所有字节累加结果的低8位。


    public  short workCmd;     //发送报文  判断类型
    public  byte workChar;   //工作结果字  判断 消息类型
    public  byte workStatus; //响应报文 工作状态 可以用来判断 报文类型



    public static int defaultSn = 0;



    public  RawMsg(){
        sn = 0;
        st = System.currentTimeMillis();
        buf = null;
    }

    public RawMsg(byte[] buf,int len){
        //Log.d("RawMsg", "RawMsg构造函数");
        sn = defaultSn++;
        st = System.currentTimeMillis();
        setBuf(buf,len);
    }

    public void Copy(RawMsg msg){
        //Log.d("RawMsg", "RawMsg构造函数");
        this.sn = msg.sn;
        this.st = msg.st;
        this.buf = msg.getBuf();
        this.length = msg.length;
        this.checkSum = msg.checkSum; //校验和（checksum）是数据长度与数据包所有字节累加结果的低8位。
        this.workCmd = msg.workCmd;     //发送报文  判断类型
        this.workChar = msg.workChar;   //工作结果字  判断 消息类型
        this.workStatus = msg.workStatus; //响应报文 工作状态 可以用来判断 报文类型
    }

    public int getSn(){
        return sn;
    }

    public void setSn(int sn){
        this.sn = sn;
    }

    public int getSt(){
        return sn;
    }

    public void setSt(int st){
        this.st = st;
    }

    public byte[] getBuf(){
        return buf;
    }

    public void setBuf(byte[] buf,int len){

        //this.st = st;
        //Log.d("RawMsg", "设置buf");

        this.buf = new byte[len];
        System.arraycopy(buf, 0, this.buf, 0, len);
    }



    public int ParseHeader(){
        //1 判断最小长度
        if(buf.length < 4){
            Log.d("RawMsg", "解析报文长度异常:"+String.valueOf(buf.length));
            return -1;
        }

        //2 判断帧头
        //Log.d("RawMsg", "解析报文头:" + String.valueOf(buf[0]) + String.valueOf(buf[1]));
        if(buf[0] != frameHead[0] || buf[1] != frameHead[1]){
            Log.d("RawMsg", "解析报文帧头失败:");
            return -1;
        }

        //3 判断有效长度
        length = buf[2];
        if(((byte)buf.length - 4) != length){
            Log.d("RawMsg", "解析报文长度异常:" + String.valueOf(buf.length) + String.valueOf(length));
            return -1;
        }

        //4 计算校验和
        int sum = length & 0xff;

        for(int i = 0;i<(length&0xff);i++ ){
            sum += buf[3+i];
        }

        checkSum =(byte)(sum &0x00FF);

        if(checkSum != buf[3+(length&0xff)]){
            Log.d("RawMsg", "解析报文校验和失败:" + String.valueOf(checkSum)+","
                    +String.valueOf(buf[3+(length&0xff)])
            );
            return -1;
        }else{
            Log.d("RawMsg", "解析报文成功");
            //大概消耗30ms 网络延时严重
            /*
            String strBuf = "";
            for(int i=0;i<buf.length;i++){
                strBuf += String.format(" %02x",buf[i]);
            }
            Log.d("RawMsg", "报文内容:"+strBuf);
            System.out.println(Arrays.toString(buf));*/
        }

        workChar = buf[3];
        workStatus = buf[4];

        return 0;
    }

    public void ToBuf(){
        buf[0] = frameHead[0];
        buf[1] = frameHead[1];

        //4 计算校验和
        int sum = length&0xff;

        for(int i = 0;i<(length&0xff);i++ ){
            sum += buf[3+i];
        }

        checkSum =(byte)(sum &0x00FF);

        buf[3+(length&0xff)] = checkSum;
    }
}
